package com.pearadmin.modules.sys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pearadmin.common.tools.SequenceUtil;
import com.pearadmin.common.tools.string.StringUtil;
import com.pearadmin.common.web.domain.request.PageDomain;
import com.pearadmin.modules.sys.domain.SysUser;
import com.pearadmin.modules.sys.domain.WorkClean;
import com.pearadmin.modules.sys.domain.dto.WorkCleanDto;
import com.pearadmin.modules.sys.domain.dto.WorkCleanListDto;
import com.pearadmin.modules.sys.mapper.WorkCleanMapper;
import com.pearadmin.modules.sys.service.IWorkCleanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 清理工作记录Service业务层处理
 *
 * @author Mr.Ling
 * @date 2021-11-26
 */
@Service
public class WorkCleanServiceImpl implements IWorkCleanService {
    @Autowired
    private WorkCleanMapper workCleanMapper;

    /**
     * 查询清理工作记录
     *
     * @param id 清理工作记录ID
     * @return 清理工作记录
     */
    @Override
    public WorkClean selectWorkCleanById(String id) {
        return workCleanMapper.selectWorkCleanById(id);
    }

    /**
     * 查询清理工作记录列表
     *
     * @param workClean 清理工作记录
     * @return 清理工作记录
     */
    @Override
    public List<WorkClean> selectWorkCleanList(WorkClean workClean) {
        return workCleanMapper.selectWorkCleanList(workClean);
    }

    /**
     * 查询清理工作记录
     * @param workClean 清理工作记录
     * @param pageDomain
     * @return 清理工作记录 分页集合
     * */
    @Override
    public PageInfo<WorkClean> selectWorkCleanPage(WorkClean workClean, PageDomain pageDomain) {
        PageHelper.startPage(pageDomain.getPage(), pageDomain.getLimit());
        List<WorkClean> data = workCleanMapper.selectWorkCleanList(workClean);
        return new PageInfo<>(data);
    }

    /**
     * 新增清理工作记录
     *
     * @param workClean 清理工作记录
     * @return 结果
     */

    @Override
    public int insertWorkClean(WorkClean workClean) {
        return workCleanMapper.insertWorkClean(workClean);
    }

    @Override
    public int batchInsertWorkClean(WorkCleanDto dto,SysUser user) throws Exception {
        List<WorkCleanListDto> list = dto.getList();
        int result = 0;
        if (!CollectionUtil.isEmpty(list)) {
            for (WorkCleanListDto wcDto : list) {
                WorkClean clean = BeanUtil.copyProperties(wcDto, WorkClean.class);
                if (StringUtil.isEmpty(clean.getAnodeCode())) {
                    throw new Exception("导杆编码不能为空");
                }
                if (StringUtil.isEmpty(clean.getBlockType())) {
                    throw new Exception("导杆类型不能为空");
                }
                if (StringUtil.isEmpty(clean.getSlotNo())) {
                    throw new Exception("槽号不能为空");
                }
                if (StringUtil.isEmpty(clean.getPoleNo())) {
                    throw new Exception("极号不能为空");
                }
                clean.setCreateTime(LocalDateTime.now());
                clean.setCreateBy(user.getUserId());
                clean.setCreateName(user.getRealName());
                clean.setId(SequenceUtil.makeStringId());
                clean.setFactory(dto.getFactory());
                clean.setWorkShop(dto.getWorkShop());
                clean.setWorkSpace(dto.getWorkSpace());
                clean.setClassInfo(dto.getClassInfo());
                clean.setFactoryName(dto.getFactoryName());
                clean.setWorkShopName(dto.getWorkShopName());
                clean.setWorkSpaceName(dto.getWorkSpaceName());
                clean.setClassInfoName(dto.getClassInfoName());
                result = workCleanMapper.insertWorkClean(clean);
            }
        }
        return result;
    }

    /**
     * 修改清理工作记录
     *
     * @param workClean 清理工作记录
     * @return 结果
     */
    @Override
    public int updateWorkClean(WorkClean workClean) {
        return workCleanMapper.updateWorkClean(workClean);
    }

    /**
     * 删除清理工作记录对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteWorkCleanByIds(String[] ids) {
        return workCleanMapper.deleteWorkCleanByIds(ids);
    }

    /**
     * 删除清理工作记录信息
     *
     * @param id 清理工作记录ID
     * @return 结果
     */
    @Override
    public int deleteWorkCleanById(String id) {
        return workCleanMapper.deleteWorkCleanById(id);
    }
}
